package com.itheima.ui;

import com.hzheima.game.GameJPanel;
import com.hzheima.util.ImageUtil;
import com.itheima.config.AppConfig;
import com.itheima.entity.Ball;
import com.itheima.entity.Bullet;
import com.itheima.entity.Plane;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class BallFightPanel extends GameJPanel {

    BufferedImage img;
    BufferedImage money;
    BufferedImage st;
    BufferedImage ed;
    Plane plane;

    boolean gameOver;

    //绘制面板
    public BallFightPanel() {
        setBackground(Color.PINK);
        img= ImageUtil.getImg("BG.png");
        money=ImageUtil.getImg("money.png");
        st=ImageUtil.getImg("st.png");
        ed=ImageUtil.getImg("ed.png");
        //创建飞机对象
        plane=new Plane();
    }


    @Override
    public void keyReleased(KeyEvent e) {
        //如果输入A则飞机向左移动
        if(e.getKeyCode()=='A'&&!gameOver){
            plane.setLeft(false);
        }

        //如果输入D则飞机向右移动
        if(e.getKeyCode()=='D'&&!gameOver){
            plane.setRight(false);
        }
    }

    //检测键盘输入内容,控制飞机移动
    @Override
    public void keyPressed(KeyEvent e) {
        super.keyPressed(e);

        //如果输入A则飞机向左移动
        if(e.getKeyCode()=='A'&&!gameOver){
            plane.setLeft(true);
        }

        //如果输入D则飞机向右移动
        if(e.getKeyCode()=='D'&&!gameOver){
            plane.setRight(true);
        }
        //重新绘制
        repaint();
    }



    @Override
    public void run() {
        if(!gameOver) {
            //发射子弹
            plane.shoot();

            //创建小球
            createBall();

            //小球移动
            moveBall();

            //子弹击中小球
            isShootBall();

            //飞机被小球击中炸毁
            isCrash();
            //重新绘制
            repaint();
        }
    }
        //如果飞机碰到小球,则飞机爆炸,结束游戏
    public void isCrash(){
        for (int i = 0; i < balls.size(); i++) {
            Ball ball=balls.get(i);

            //胖多飞机是否与小球碰撞
            if(plane.beCrash(ball)) {
                balls.remove(ball);
                gameOver=false;
            }
        }
    }


    //击中小球
    public void isShootBall(){

        //遍历小球集合
        for (int i =balls.size()-1; i>=0; i--) {
            Ball ball=balls.get(i);
            ArrayList<Bullet>bullets=plane.getArrayList();

            for (int j = bullets.size()-1; j >=0 ; j--) {
               Bullet bullet= bullets.get(j);

               //判断是否击中
                if(ball.beShoot(bullet)){
                    //
                    bullets.remove(bullet);
                    if(ball.getBlood()<=0){
                        balls.remove(ball);
                       // break;
                    }
                    //如果血量降低为原来的一半,小球分裂
                  if(ball.getBlood()<=ball.getInitBlood()/2){
                      ArrayList<Ball>newBall=ball.split();
                        balls.remove(ball);
                        balls.addAll(newBall);

                  }
                }
            }
        }
    }



    public void moveBall(){
        for (int i = 0; i < balls.size(); i++) {
           Ball ball=balls.get(i);
           ball.move();
           if(ball.getY()>AppConfig.App_HIGHT+ball.getHight()){
                balls.remove(i);
            }
        }
    }
    //创建小球集合
    ArrayList<Ball>balls=new ArrayList<>();
    int count;
    public void createBall(){
        count++;
        if(count%20==0) {
            if(balls.size()<=AppConfig.BALL_MAX_COUNT){
                Ball ball = new Ball();
                balls.add(ball);
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        //画面板
        g.drawImage(img,0,0,AppConfig.App_WIDTH,AppConfig.App_HIGHT,null);
        g.setFont(new Font("宋体",Font.ITALIC,25));
        g.setColor(Color.WHITE);
        g.drawString("分数:0",30,30);
        g.drawString("历史最高:0", 300, 60);
        g.drawImage(money, 300, 10, 30, 30, null);
        g.drawString("0.00k", 340, 32);
        g.drawString("关卡1",30,60);

        //绘制关卡进度条
        g.drawImage(st,20,80,5,25,null);

        g.setColor(Color.gray);
        for (int i = 0; i < 30; i++) {
            g.fillRect(30+i*15,80,10,25);
        }


        g.setColor(Color.cyan);
        for (int i = 0; i <5; i++) {
            g.fillRect(30+i*15,80,10,25);
        }
        g.drawImage(ed, AppConfig.App_WIDTH-30,80,5,25,null);

        //创建飞机
        plane.draw(g);
        plane.getBullets();
        for (int i = 0; i <plane.getArrayList().size() ; i++) {
            Bullet bullet=plane.getArrayList().get(i);
            bullet.draw(g);
        }

        //绘制小球
        for (int i = 0; i < balls.size(); i++) {
            Ball ball =balls.get(i);
            ball.draw(g);
            System.out.println(i);
        }

        if(gameOver){
            //绘制半透明
            g.setColor(Color.red);
            Graphics2D g2d=(Graphics2D)g;
            //设置半透明
            AlphaComposite alphaComposite=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.42f);
            g2d.setComposite(alphaComposite);
            g2d.fillRect(0,200,AppConfig.App_WIDTH,300);


            /*
            * 设置结束提示
            * */
            //设置不透明
            AlphaComposite alphaComposite2=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f);
            g2d.setComposite(alphaComposite2);
            g.setColor(Color.white);

            g.setFont(new Font("楷体",Font.BOLD,30));
            g.drawString("游戏结束",180,240);

            //
            g.setColor(Color.yellow);
            g.drawString("本次得分:0",150,300);
            g.drawString("本次获得金币:",100,370);
            g.drawImage(money,250,340,40,40,null);
            g.drawString("0.00k",300,370);

            //重新开始
            g.setColor(Color.white);
            g.drawString("{按F1重新开始}",80,480);
        }
    }


}
